﻿#region MIT Licenze
//The MIT License

//Copyright (c) 2008 Sebastian Faltoni, dotnetfireball.org

//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace Fireball.OpenXML.Word
{
    public sealed class LatentStyleException : OpenXMLEntity
    {
        /// <summary>
        /// The name of this Exception
        /// </summary>
        public new string Name
        {
            get;
            set;
        }

        /// <summary>
        /// Semi Hidden Text Override
        /// </summary>
        public bool? SemiHidden
        {
            get;
            set;
        }

        /// <summary>
        /// Locked Style
        /// </summary>
        public bool? Locked
        {
            get;
            set;
        }

        /// <summary>
        /// 
        /// </summary>
        public bool? PrimaryStyle
        {
            get;
            set;
        }

        /// <summary>
        /// Decimal value ui priority sorting number
        /// </summary>
        public decimal? UIPriority
        {
            get;
            set;
        }

        /// <summary>
        /// Unhide when the style is used
        /// </summary>
        public bool? UnHideWhenUsed
        {
            get;
            set;
        }



        public override System.Xml.Linq.XContainer ToXml()
        {
            if (string.IsNullOrEmpty(this.Name))
                throw new Exception("LatentStyleException Name Property can't be null");

            XElement lsdException = new XElement(NS.WM + "lsdException");
            
            lsdException.Add(new XAttribute(NS.WM + "name", this.Name));

            if (this.Locked != null)
                lsdException.Add(new XAttribute(NS.WM + "locked", this.Locked.Value.ToOnOff()));

            if (this.PrimaryStyle != null)
                lsdException.Add(new XAttribute(NS.WM + "qFormat", this.PrimaryStyle.Value.ToOnOff()));

            if (this.SemiHidden != null)
                lsdException.Add(new XAttribute(NS.WM + "semiHidden", this.SemiHidden.Value.ToOnOff()));

            if (this.UIPriority != null)
                lsdException.Add(new XAttribute(NS.WM + "uiPriority", this.UIPriority));

            if (this.UnHideWhenUsed != null)
                lsdException.Add(new XAttribute(NS.WM + "unhideWhenUsed", this.UnHideWhenUsed.Value.ToOnOff()));

            return lsdException;
        }

        private void ParseInternal(XElement child)
        {
            if (child.Name.LocalName != "lsdException")
                throw new Exception("Invalid Node lsdException");

            var name = child.Attribute(NS.WM + "name");

            if (name != null)
                this.Name = name.Value;

            var locked = child.Attribute(NS.WM + "locked");

            if (locked != null)
            {
                this.Locked = locked.Value.ToBoolean();
            }

            var qFormat = child.Attribute(NS.WM + "qFormat");

            if (qFormat != null)
                this.PrimaryStyle = qFormat.Value.ToBoolean();

            var semiHidden = child.Attribute(NS.WM + "semiHidden");

            if (semiHidden != null)
                this.SemiHidden = semiHidden.Value.ToBoolean();

            var uiPriority = child.Attribute(NS.WM + "uiPriority");

            if (uiPriority != null)
                this.UIPriority = Convert.ToDecimal(uiPriority.Value);

            var unhideWhenUsed = child.Attribute(NS.WM + "unhideWhenUsed");

            if (unhideWhenUsed != null)
                this.UnHideWhenUsed = unhideWhenUsed.Value.ToBoolean();
        }

        public static LatentStyleException Parse(XElement child)
        {
            var s = new LatentStyleException();
            s.ParseInternal(child);
            return s;
        }
    }
}
